package cc.fs.zhihuapp;

import android.app.Application;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

import cc.fs.zhihuapp.rest.asynchttp.ApiHelper;
import cc.fs.zhihuapp.utils.DatabaseHelper;

/**
 * Created by fostion on 2015/9/6.
 * 异常捕捉（及显示错误页）
 */
public class App extends Application implements Thread.UncaughtExceptionHandler {

    public static App instance;
    private static RefWatcher refWatcher;
    private DatabaseHelper databaseHelper;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //iamge init
        AppConfig.init(this);
        ApiHelper.init();
        Fresco.initialize(this);
        refWatcher = LeakCanary.install(this);
        databaseHelper = new DatabaseHelper(this);

        //处理为处理异常
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    public static RefWatcher getRefWatcher() {
        return refWatcher;
    }

    public DatabaseHelper getDatabaseHelper() {
        return databaseHelper;
    }

    /*************************************************
     * 处理未知错误
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        //todo 错误在这里处理及显示
        handlerError();
        System.exit(0);
    }

    public void handlerError() {
        //发送广播使用broardcast或者avtivity 重启
    }

}
